#include "npc4.h"
#include "../effect.h"
#include "path.h"
#include "../gun/gun.h"

namespace fireplane{
	NPC4::NPC4(EnemyType type):
		Enemy(type),_fire_tick(0),
		_use_left_gun(false),
		_fire_middle_gun_tick(0),
		_is_deading(false)
	{}

	bool NPC4::updateState( float dt ){
		bool ret = true;

		if (isDestroy())
		{
			return false;
		}

		if (_is_attak){
			_attack_back_time += dt;
			if (_attack_back_time > 0.5f){
				_attack_back_time = 0;
				_is_attak = false;
				_sprite->setColor(ccc3(255,255,255));
			}
		}

		CCPoint pos = this->getPosition();
		if (pos.y < -50){
			ret =  false;
		}

		list<CCRect>::iterator it = _all_collision_rects.begin();
		for (;it != _all_collision_rects.end();it ++){
			CCRect& rect = (*it);
			rect.origin.x = pos.x - rect.size.width / 2;
			rect.origin.y = pos.y - rect.size.height / 2;
		}

		if(!_is_deading){
			_fire_tick += dt;
			if(_fire_tick > 2.0f){
				_fire_tick = 0;

				if(_use_left_gun)
					_gun[0]->fire(Bullet::BI_img2);
				else
					_gun[1]->fire(Bullet::BI_img2);

				_use_left_gun = !_use_left_gun;
			}

			_fire_middle_gun_tick += dt;
			if(_fire_middle_gun_tick > 2.5f){
				_fire_middle_gun_tick = 0;
				_gun[2]->fire(Bullet::BI_img3);
			}
		}
		else{
			if(0 == _sprite->numberOfRunningActions()){
				setDestroyFlag(true);

				// create effect
				CCPoint worldpos = this->getPosition();
				worldpos = this->getParent()->convertToWorldSpace(worldpos);

				Effect::CreateNpc4Bomb0 cmd;
				cmd._id = Effect::ET_npc41;
				cmd.worldPos = worldpos;

				Effect::createEffect(&cmd);
			}
		}
		
		return ret;
	}

	NPC4::~NPC4(){}

	void NPC4::onInit(CreateArg* arg)
	{
		CreateNormalEnemy* targ = (CreateNormalEnemy*)arg;

		_sprite = CCSprite::create("enemy/npc4/body.png");
		addChild(_sprite);
		this->setPosition(targ->pos);

		CCMoveBy* m0 = CCMoveBy::create(0.1f,ccp(0,-5));
		CCMoveBy* m1 = CCMoveBy::create(0.1f,ccp(0,5));
		CCSequence* seq = CCSequence::create(m0,m1,NULL);
		CCRepeatForever* action = CCRepeatForever::create(seq);
		_sprite->runAction(action);

		CCSize contentsize = _sprite->getContentSize();
		CCRect rect = CCRectMake(targ->pos.x - contentsize.width / 2,targ->pos.y - contentsize.height / 2,contentsize.width,
			contentsize.height);

		_all_collision_rects.push_back(rect);

		_hp = 100;
		_is_attak = false;
		_attack_back_time = 0;
		_cur_step = 1;
		_speed = 200.0f;

		Gun::CreateNormalGun arg0(Gun::CT_circle_gun);
		arg0.tdir = ccp(0,-1);
		arg0.tspeed = 200;
		arg0.o = Bullet::BO_ENEMY;
		arg0.pos = ccp(-55,-100);

		_gun[0] = Gun::createGun(&arg0);

		Gun::CreateNormalGun arg1(Gun::CT_circle_gun);
		arg1.tdir = ccp(0,-1);
		arg1.tspeed = 200;
		arg1.o = Bullet::BO_ENEMY;
		arg1.pos = ccp(55,-100);

		_gun[1] = Gun::createGun(&arg1);

		addChild(_gun[0]);
		addChild(_gun[1]);


		Gun::CreateNormalGun arg2(Gun::GT_normal_gun);
		arg2.tdir = ccp(0,-1);
		arg2.tspeed = 250;
		arg2.o = Bullet::BO_ENEMY;
		arg2.pos = ccp(0,-50);

		_gun[2] = Gun::createGun(&arg2);
		addChild(_gun[2]);
	}

	void NPC4::destroy()
	{
		Enemy::destroy();
	}

	void NPC4::onCollision2Player(){
		if(_is_deading){
			return;
		}

		_onAttack(-_hp);
	}

	void NPC4::_onAttack(int addHp){
		if(_is_deading){
			return;
		}

		_hp += addHp;
		if (_hp <= 0)
		{
			_hp = 0;
			_is_deading = true;
			_sprite->stopAllActions();

			// create effect
			CCPoint worldpos = this->getPosition();
			worldpos = this->getParent()->convertToWorldSpace(worldpos);

			Effect::CreateNpc4Bomb0 cmd;
			cmd._id = Effect::ET_npc4;
			cmd.worldPos = worldpos;
			
			Effect::createEffect(&cmd);
		}

		if(!_is_deading){
			_sprite->setColor(ccc3(180,0,0));
			_is_attak = true;
			_attack_back_time = 0;
		}
		else{
			CCPoint pos = _sprite->getPosition();
			_sprite->initWithFile("enemy/npc4/destroy.png");
			_sprite->setPosition(pos);

			pos.x += 10;
			pos.y -= 10;
			CCMoveTo* m0 = CCMoveTo::create(0.4f,pos);
			_sprite->runAction(m0);
			_sprite->runAction(CCScaleTo::create(0.4f,0.5f));
		}
	}

	void NPC4::onAttack()
	{
		if(_is_deading){
			return;
		}

		_onAttack(-10);
	}
}